home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Programmer's Power Pack
/
Delphi Volume 1.iso
/
s_to_z
/
sndkey11
/
smain.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-09-15
|
5KB
|
112 lines
unit Smain;
{ Example program demontrating the use of SNDKEYxx.DLL }
{ Copyright ⌐ 1996, J M Technical Services. All rights reserved. }
interface
uses
WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
SendBtn: TButton;
procedure SendBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{ Add SendKey to your uses statement to include the definitions for
the SK_ constants and the SendKeyString function. You must distribute
SNDKEYxx.DLL with your application, either in the same directory as
the executable or in the \windows\system directory. }
uses SendKey;
{$R *.DFM}
procedure Delay(ms : LongInt);
begin
ms := GetTickCount + ms;
while (GetTickCount < ms) do Application.ProcessMessages;
end;
procedure TForm1.SendBtnClick(Sender: TObject);
var
Handle, WHandle: hWnd;
i: Integer;
ok: Boolean;
begin
SendBtn.Enabled := false;
{ Run Notepad }
if WinExec('NOTEPAD.EXE',SW_SHOW) >= 32 then
begin
{ Get the handle of the active window (Notepad)
by waiting until the active window handle changes }
repeat
Application.ProcessMessages;
Handle := FindWindow(PChar(0),'Untitled - Notepad');
until Handle <> 0;
{ Send the text to the window identified by Handle.
Use the SK_ constants to insert non-printing characters into the key string.
These constants have the ASCII values 228 - 255, so the ASCII characters
represented by these values cannot be included. }
if SendKeyString(Handle,'This is a demonstration of the SendKeys DLL'+SK_ENTER) then
{ The function returns 'false' if it fails. The trial run version will fail if
it is run when the Delphi environment is not active. }
begin
{ Keystrokes can only be sent to the active window which has input focus.
We pass Handle to the procedure so it can make sure the window is active. }
ok := SendKeyString(Handle,'for Borland Delphi. It will cost you just $15.00'+SK_ENTER);
if ok then ok := SendKeyString(Handle,'to register this software.'+SK_ENTER+SK_ENTER);
{ Press the shift key for the whole of the next line to send as capitals. }
if ok then ok := SendKeyString(Handle,SK_SHIFT_DN+'In this line the Shift key is down'+SK_SHIFT_UP+SK_ENTER+SK_ENTER);
if ok then ok := SendKeyString(Handle,'Now we will press F1 to look at the help file.'+SK_ENTER);
if ok then ok := SendKeyString(Handle,SK_F1);
{ WinHelp is a different application, so we must get its handle before
sending keystrokes. }
repeat
Application.ProcessMessages;
WHandle := FindWindow(PChar(0),'Help Topics: Notepad Help');
until (WHandle <> 0);
{ This next sequence will display the help on printing
in the Win 95 Notepad's help file. }
if ok then ok := SendKeyString(WHandle,SK_DOWN+SK_DOWN+SK_ENTER+SK_DOWN+SK_ENTER);
if not ok then exit;
{ Leave the help window for a few seconds so you can see it! }
Delay(2000);
{ Now close WinHelp. }
{ Note the use of SK_ALT_DN and SK_ALT_UP to represent holding the Alt key down. }
if ok then ok := SendKeyString(WHandle,SK_ALT_DN+SK_F4+SK_ALT_UP);
{ Wait for WinHelp to close. }
Delay(1000);
{ Send some more text. }
if ok then ok := SendKeyString(Handle,SK_ENTER+'That was easy!'+SK_ENTER+SK_ENTER);
if ok then ok := SendKeyString(Handle,SK_ENTER+'Now lets try a find and replace'+SK_ENTER+SK_ENTER);
if ok then ok := SendKeyString(Handle,SK_CTRL_DN+SK_HOME+SK_UP);
if ok then ok := SendKeyString(Handle,SK_ALT_DN+'S'+SK_ALT_UP+'FDelphi'+SK_ENTER);
if ok then SendMessage(FindWindow(PChar(0),'Find'),WM_CLOSE,0,0);
if ok then ok := SendKeyString(Handle,SK_CTRL_DN+SK_END+SK_UP);
if ok then ok := SendKeyString(Handle,'Now save the text and close Notepad.'+SK_ENTER);
{ Send the keystrokes to save the file as TEST.TXT }
if ok then ok := SendKeyString(Handle,SK_ALT_DN+'FS'+SK_ALT_UP+'TEST.TXT'+SK_ENTER);
{ Possibly the file already exists, so deal with OK to overwrite msg .. }
WHandle := FindWindow(PChar(0),'Save As');
if WHandle <> 0 then ok := SendKeyString(WHandle,SK_LEFT+SK_ENTER);
{ .. and close Notepad. NB: closing an application you are sending keystrokes to }
{ can lock up the sending application, so do it this way ... }
if ok then SendMessage(Handle,WM_CLOSE,0,0);
end;
SendBtn.Enabled := true;
end;
end;
end.